<?php
class ManageAction extends CommonAction {

    public function index() {
        $this->_isSuperAdmin();
        $admin = D('Admin');
        //搜索模块
        parent::search(array(
            'a.name'=>'管理员名字',
        ));

        if(!isset($_GET['act'])) {
            parent::page($admin->count());       //分页
            $manages = $admin->alias('a')
                             ->field('l.name as level_name,a.id,a.name,a.last_ip,a.last_date')
                             ->join('xinxi_level l ON l.id=a.level')
                             ->order('level')
                             ->limit($this->limit)
                             ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($admin->alias('a')->where($this->map)->count());       //分页
            $manages = $admin->alias('a')
                             ->field('l.name as level_name,a.id,a.name,a.last_ip,a.last_date')
                             ->join('xinxi_level l ON l.id=a.level')
                             ->where($this->map)
                             ->order('level')
                             ->limit($this->limit)
                             ->select();
        }
        $this->assign('empty', '<tr><td colspan="6">没有任何数据</td></tr>');
        $this->assign('allmanage', $manages);

        $this->display();
    }

    public function add() {
        $this->_isSuperAdmin();
        $admin = D('Admin');
        $level = D('Level');
        if(isset($_POST['send'])) {
            if($admin->create()) {
                $admin->add() ? $this->success('添加管理员成功!', __URL__) : $this->error('系统出错，添加管理员失败!', 0);
            } else {
                $this->error($admin->getError());
            }
        }
        $this->assign('level', $level->select());
        $this->display();
    }

    public function update() {
        $this->_isSuperAdmin();
        $id = $this->_get('id');
        if(isset($id)) {
            $admin = D('Admin');
            $level = D('Level');
            if(isset($_POST['send'])) {
                if($admin->create()) {
                    $admin->save() ? $this->success('修改管理员成功!', __URL__) : $this->error('系统出错，修改管理员失败!', 0);
                } else {
                    $this->error($admin->getError());
                }
            }
            if(!$admin->field('id')->where('id='.$id)->find()) $this->error('不存在此数据!', 0);
            $this->assign('manage',$admin->field(array('id','name','pass','level'))->where('id='.$id)->find());
            $this->assign('level', $level->field(array('id','name'))->select());
            $this->display();
        }

    }

    public function delete() {
        $this->_isSuperAdmin();
        $id = $this->_get('id');
        $admin = D('Admin');
        $admin_name = $admin->field('name')->where('id='.$id)->find();
        if($admin_name['name'] == session('admin_name')) $this->error('不能删除自己!', 0);
        if(!$admin->field('id')->where('id='.$id)->find()) $this->error('不存在此数据', 0);
        $admin->where('id='.$id)->delete() ? $this->success('删除管理员成功!', __URL__) : $this->error('系统出错，删除管理员失败!', 0);
    }

    public function mulDelete() {
        $this->_isSuperAdmin();
        $admin = D('Admin');
        $ids = implode(',', $this->_post('select'));
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $map['id'] = array('in',$ids);
        $admins = $admin->field('name')->where($map)->select();
        foreach ($admins as $key=>$value) {
            if($value['name'] == session('admin_name')) $this->error('不能删除自己!', 0);
        }
        $admin->where($map)->delete() ? $this->success('批量删除管理员成功!', __URL__) : $this->error('系统出错，批量删除管理员失败!', 0);
    }

    //ajax验证用户名
    public function uniqueName() {
        $admin = D('Admin');
        $name = $this->_post('name');
        echo $admin->where("name='$name'")->getField('id') ? 1 : 0;
    }

    private function _isSuperAdmin() {
        if(session('admin_level') !== '超级管理员') $this->error('权限不足,仅允许超级管理员进行操作!', 0);
    }



}


?>